package com.easytext.resourcebundle.china;

import com.easytext.resourcebundle.main.spi.TranslationsProvider;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.spi.AbstractResourceBundleProvider;

/**
 * @author caifan
 * @created 2021/12/4
 * @description:
 */
public class ChinaTranslationsProvider extends AbstractResourceBundleProvider implements TranslationsProvider {
    private static Locale CHINA = Locale.forLanguageTag("zh_CN");

    @Override
    public String toBundleName(String baseName, Locale locale) {
        String bundleName = super.toBundleName(baseName, locale);
        if (CHINA.equals(locale)) {
            int index = bundleName.lastIndexOf('.');
            return bundleName.substring(0, index + 1) + "china" + bundleName.substring(index);
        }
        return bundleName;
    }

    @Override
    public ResourceBundle getBundle(String baseName, Locale locale) {
        if (CHINA.equals(locale)) {
            return super.getBundle(baseName, locale);
        }
        return null;
    }
}
